import type { Restaurant, Cuisine } from './types';
import { searchNearbyRestaurants } from './amap';
import { debounce } from './debounce';

const CUISINES: Cuisine[] = [
  '川菜',
  '湘菜',
  '粤菜',
  '日料',
  '韩料',
  '西餐',
  '快餐',
  '火锅',
  '麻辣烫',
  '轻食',
];

export { CUISINES };

const MOCK: Restaurant[] = [
  {
    id: 'r-1',
    name: '川味小馆',
    // imageUrl: '/sichuan-food.jpg', // 不再需要图片URL
    cuisines: ['川菜'],
    source: 'nearby',
    avgPrice: 40,
    rating: 4.5,
    etaMinutes: 10,
    mapUrl: 'https://uri.amap.com/search?keyword=%E5%B7%9D%E5%91%B3%E5%B0%8F%E9%A6%86',
  },
  {
    id: 'r-2',
    name: '湘味人家',
    // imageUrl: '/hunan-food.jpg', // 不再需要图片URL
    cuisines: ['湘菜'],
    source: 'nearby',
    avgPrice: 38,
    rating: 4.3,
    etaMinutes: 12,
    mapUrl: 'https://uri.amap.com/search?keyword=%E6%B9%98%E5%91%B3%E4%BA%BA%E5%AE%B6',
  },
  {
    id: 'r-3',
    name: '粤食坊',
    // imageUrl: '/cantonese-food.jpg', // 不再需要图片URL
    cuisines: ['粤菜'],
    source: 'delivery',
    avgPrice: 55,
    rating: 4.7,
    etaMinutes: 30,
    detailUrl: 'https://www.meituan.com/',
  },
  {
    id: 'r-4',
    name: '寿司屋',
    // imageUrl: '/japanese-food.jpg', // 不再需要图片URL
    cuisines: ['日料'],
    source: 'delivery',
    avgPrice: 68,
    rating: 4.6,
    etaMinutes: 35,
    detailUrl: 'https://www.ele.me/',
  },
  {
    id: 'r-5',
    name: '韩式拌饭',
    // imageUrl: '/korean-food.jpg', // 不再需要图片URL
    cuisines: ['韩料'],
    source: 'nearby',
    avgPrice: 32,
    rating: 4.2,
    etaMinutes: 8,
    mapUrl: 'https://uri.amap.com/search?keyword=%E9%9F%A9%E5%BC%8F%E6%8B%8C%E9%A5%AD',
  },
  {
    id: 'r-6',
    name: '西餐牛排',
    // imageUrl: '/western-food.jpg', // 不再需要图片URL
    cuisines: ['西餐'],
    source: 'delivery',
    avgPrice: 88,
    rating: 4.8,
    etaMinutes: 45,
    detailUrl: 'https://www.dianping.com/',
  },
  {
    id: 'r-7',
    name: '麻辣烫王',
    // imageUrl: '/malatang.jpg', // 不再需要图片URL
    cuisines: ['麻辣烫'],
    source: 'nearby',
    avgPrice: 28,
    rating: 4.1,
    etaMinutes: 6,
    mapUrl: 'https://uri.amap.com/search?keyword=%E9%BA%BB%E8%BE%A3%E7%83%AB',
  },
  {
    id: 'r-8',
    name: '轻食沙拉',
    // imageUrl: '/light-meal.jpg', // 不再需要图片URL
    cuisines: ['轻食'],
    source: 'delivery',
    avgPrice: 36,
    rating: 4.4,
    etaMinutes: 25,
    detailUrl: 'https://www.ele.me/',
  },
  {
    id: 'r-9',
    name: '快餐便当',
    // imageUrl: '/fast-food.jpg', // 不再需要图片URL
    cuisines: ['快餐'],
    source: 'nearby',
    avgPrice: 24,
    rating: 3.9,
    etaMinutes: 5,
    mapUrl: 'https://uri.amap.com/search?keyword=%E4%BE%BF%E5%BD%93',
  },
  {
    id: 'r-10',
    name: '重庆火锅',
    // imageUrl: '/hot-pot.jpg', // 不再需要图片URL
    cuisines: ['火锅'],
    source: 'nearby',
    avgPrice: 75,
    rating: 4.6,
    etaMinutes: 15,
    mapUrl: 'https://uri.amap.com/search?keyword=%E7%81%AB%E9%94%85',
  },
];

export async function fetchNearby(longitude?: number, latitude?: number): Promise<Restaurant[]> {
  // 如果提供了坐标，使用高德地图API搜索
  if (longitude !== undefined && latitude !== undefined) {
    try {
      const restaurants = await searchNearbyRestaurants(longitude, latitude);
      return restaurants;
    } catch (error) {
      console.error('高德地图API搜索失败，使用模拟数据:', error);
      // 如果API失败，回退到模拟数据
      await delay(API_DELAY_MS);
      return MOCK.filter((r) => r.source === 'nearby');
    }
  }

  // 没有坐标时使用模拟数据
  await delay(API_DELAY_MS);
  return MOCK.filter((r) => r.source === 'nearby');
}

export async function fetchDelivery(): Promise<Restaurant[]> {
  // Simulate network delay for realistic loading experience
  await delay(API_DELAY_MS);
  return MOCK.filter((r) => r.source === 'delivery');
}

export async function fetchAll(longitude?: number, latitude?: number): Promise<Restaurant[]> {
  const [nearby, delivery] = await Promise.all([fetchNearby(longitude, latitude), fetchDelivery()]);
  return [...nearby, ...delivery];
}

// 创建防抖版本的fetchAll函数
export const debouncedFetchAll = debounce(fetchAll, 500);

const API_DELAY_MS = 150;

function delay(ms: number) {
  return new Promise((res) => setTimeout(res, ms));
}
